package com.cmwa.scbp.sys.permission.post.service.impl;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import com.cmwa.scbp.base.mvc.entity.Page;
import com.cmwa.scbp.base.mvc.entity.SearchParam;
import com.cmwa.scbp.base.exception.RepositoryException;
import com.cmwa.scbp.sys.permission.post.dao.PostDao;
import com.cmwa.scbp.sys.permission.post.entity.PostVo;
import com.cmwa.scbp.sys.permission.post.service.PostService;
import com.cmwa.scbp.base.constants.Constant;
import com.cmwa.scbp.utils.SessionUtils;

@Service
public class PostServiceImpl implements PostService {

	@Resource
	private PostDao postDao;
	
	@Override
	public PostVo detailById(String postId) {
		return postDao.detailById(postId);
	}

	@Override
	public void savePost(PostVo postVo) throws RepositoryException{
		// 去空格
		if (postVo != null) {
			postVo.getPostTitle().trim();
			postVo.getPostCode().trim();
		}
		//登录人ID
		postVo.setCreateId(SessionUtils.getEmployee().getID());
		// 数据状态：已审核
		postVo.setStatus(Constant.C_STATUS_C);
		postDao.savePost(postVo);
	}

	@Override
	public void updatePost(PostVo postVo) throws RepositoryException{
		// 去空格
		if (postVo != null) {
			postVo.getPostTitle().trim();
			postVo.getPostCode().trim();
		}
		//登陆人ID
		postVo.setModifyId(SessionUtils.getEmployee().getID());
		postDao.updatePost(postVo);
	}

	@Override
	public boolean checkRepeatPost(String postTitle, String postCode,
			String postId) {
		// 保存参数
		HashedMap param = new HashedMap();
		param.put("postTitle", postTitle);
		// 因为不区分大小写，统一转大写查询
		param.put("postCode", postCode.toUpperCase());//字母转大写
		param.put("postId", postId);
		int count = postDao.getCount(param);
		return count > 0;
	}

	@Override
	public Page<PostVo> postListPage(SearchParam param) {
		//去空格
		String postTitle = (String)param.getSp().get("postTitle");
		if (postTitle != null && postTitle != "") {
			postTitle = postTitle.trim();
		}
		param.getSp().put("postTitle", postTitle);
		List<PostVo> items = postDao.postListPage(param);
		return Page.create(items, param.getStart(), param.getLimit(), param.getTotal());
	}

	@Override
	public void deletePostWithStatus(String postId) throws RepositoryException{
		PostVo postVo = new PostVo();
		postVo.setPostId(postId);
		// 数据状态：删除
		postVo.setStatus(Constant.C_STATUS_D);
		// 软删除
		postDao.updatePost(postVo);
	}

	@Override
	public List<PostVo> queryMatchPost(String postTitle) {
		return postDao.queryMatchPost(postTitle);
	}

	/**
	 * 查询岗位信息
	 */
	@Override
	public PostVo queryPostInfo(PostVo postVo) {
		return postDao.queryPostInfo(postVo);
	}

	@Override
	public Set<String> getPostCodesByEmpId(String empId) {
		return postDao.getPostCodesByEmpId(empId);
	}
	
	@Override
	public List<PostVo> queryIsConfPostList() {
		return postDao.queryIsConfPostList();
	}
}
